const fs = require("fs");
const fileService = require("../service/file.service");
const userService = require("../service/user.service");
const { UPLOAD_PATH } = require("../config/path");

class UserController {
  async create(ctx, next) {
    const user = ctx.request.body;

    const result = await userService.create(user);

    ctx.body = {
      message: "创建用户成功",
      data: result,
    };
  }

  async showAvatarImage(ctx, next) {
    const { userId } = ctx.params;

    const avatarInfo = await fileService.queryAvatarWithUserId(userId);

    const { filename, mimetype } = avatarInfo;
    ctx.type = mimetype;
    ctx.body = fs.createReadStream(`${UPLOAD_PATH}/${filename}`); // fs.createReadStream()读取文件流
  }
}

module.exports = new UserController();
